package com.huang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.huang.pojo.User;
import com.huang.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@Controller走视图解析器
@RestController//加这个注释就不会走视图解析器，直接返回字符串和ResponseBody一样
public class UserController {
    //乱码问题：@RequestMapping(value = "/t1",produces = "application/json;charset=utf-8")
    //可以加入prodect属性也可以在springmvc-servlet.xml文件中统一配置
    @RequestMapping(value = "/t1")
//    @ResponseBody //加这个注释就不会走视图解析器，直接返回字符串
    public String test1() throws JsonProcessingException {
        //jackson，ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        //创建对象
        User user = new User(1, "小明", 18);
        String s = mapper.writeValueAsString(user);
        return s;
    }

    @RequestMapping("/t2")
    public String test2() throws JsonProcessingException {
        List<User> user = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        User user1 = new User(1, "小明", 18);
        User user2 = new User(2, "小红", 19);
        User user3 = new User(3, "小李", 18);
        User user4 = new User(4, "小黑", 11);
        User user5 = new User(5, "小猪", 12);
        user.add(user1);
        user.add(user2);
        user.add(user3);
        user.add(user4);
        user.add(user5);
        return JsonUtils.getJson(user);
    }

    @RequestMapping("/t3")
    public String test3() throws JsonProcessingException {
//        ObjectMapper mapper = new ObjectMapper();
//        //不使用时间戳的方式
//        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//        //自定义日期格式对象
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////        java实现sdf.format(date)
//        //指定日期格式
//        mapper.setDateFormat(sdf);
//        Date date = new Date();
//        String s = mapper.writeValueAsString(date);
//        return s;
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //使用工具类jsonUtils
        Date date=new Date();
        return JsonUtils.getJson(date);
    }

    @RequestMapping("/t4")
    public String test4(){
        List<User> user = new ArrayList<>();
        User user1 = new User(1, "小明", 18);
        User user2 = new User(2, "小红", 19);
        User user3 = new User(3, "小李", 18);
        User user4 = new User(4, "小黑", 11);
        User user5 = new User(5, "小猪", 12);
        user.add(user1);
        user.add(user2);
        user.add(user3);
        user.add(user4);
        user.add(user5);
        System.out.println("*******Java对象 转 JSON字符串*******");
        String str1 = JSON.toJSONString(user);
        System.out.println("JSON.toJSONString(list)==>"+str1);
        String str2 = JSON.toJSONString(user1);
        System.out.println("JSON.toJSONString(user1)==>"+str2);
        System.out.println("\n****** JSON字符串 转 Java对象*******");
        User jp_user1=JSON.parseObject(str2,User.class);
        System.out.println("JSON.parseObject(str2,User.class)==>"+jp_user1);
        System.out.println("\n****** Java对象 转 JSON对象 ******");
        JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
        System.out.println("(JSONObject) JSON.toJSON(user2)==>"+jsonObject1.getString("name"));
        System.out.println("\n****** JSON对象 转 Java对象 ******");
        User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
        System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user);
        return "yes";
    }
}
